/**
 * 
 */
package com.nari.baseinfo.action;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IFileMapping;
import com.nari.baseinfo.model.IHeatRoom;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.IBaseStatisService;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.IBuildingUnitService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IFileMappingService;
import com.nari.baseinfo.service.IHeatRoomService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.baseinfo.utils.BaseStatisConstants;
import com.nari.baseinfo.utils.statisentity.IGenericStatisEntity;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class BuildingAction extends JSONActionSupport {
	private static final long serialVersionUID = -7200290032054612206L;
	private IBuildingService iBuildingService;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IFileMappingService iFileMappingService;
	private IHeatRoomService iHeatRoomService;
	private IBuildingUnitService iBuildingUnitService;
	private IBaseStatisService iBaseStatisService;
	// 楼栋信息查询
	private String buildingName;
	private String projectUID;
	private String communityUID;
	private List<IProject> projectList;
	private List<ICommunity> communityList;
	private Map<String, String> pageProjectMap; // 所属项目id-projectName映射表
	private Map<String, String> pageCommunityMap; // 所属小区id-communityName映射表
	private List<IHeatRoom> heatRoomList;
	// 分页查询
	private int pageIndex = 1; // 默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<IBuilding> buildingPage;
	// 楼栋信息管理
	private String buildingUID;
	private IBuilding building;
	private IFileMapping attachFileMap;
	// 楼栋附件
	private File attachFile;
	private String attachFileFileName;
	// 通过楼栋所属关系查询楼栋列表
	private List<IBuilding> buildingList;
	//楼栋统计信息
	private IGenericStatisEntity statisEntity;
	private IProject project;
	private ICommunity community;
	
	/**
	 * 楼栋信息管理导航
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-26
	 */
	public String queryBuildingMgrNav() throws Exception {
		//便于系统参数携带
		return "buildingMgrNav";
	}

	/**
	 * 分页查询楼栋列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-8
	 */
	public String queryBuildingPage() throws Exception {
		// 所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if (this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		// 所属小区
		if (this.projectUID != null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		// 分页结果
		this.buildingPage = this.iBuildingService.queryBuildingPage(buildingName, projectUID, communityUID, pageCond);
		// 计算结果集的页数
		this.pageSize = new BigDecimal(this.buildingPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.buildingPage.getPageCond().getLimit()),RoundingMode.UP).intValue();
		if (pageSize == 0) pageSize = 1;
		// 所属项目映射表
		this.pageProjectMap = new HashMap<String, String>();
		for (IProject project : this.projectList) {
			this.pageProjectMap.put(project.getId(), project.getProjectName());
		}
		// 查询所属小区映射表
		this.pageCommunityMap = this.iBuildingService.queryReferCommunityMap(buildingPage.getList());
		return "buildingPage";
	}

	/**
	 * 楼栋信息管理
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-8
	 */
	public String queryBuildingForManage() throws Exception {
		// 楼栋信息
		if (this.buildingUID != null && !"".equals(buildingUID)) {
			this.building = this.iBuildingService.queryEntityById(buildingUID);
			this.projectUID = this.building.getProjectID();
			this.communityUID = this.building.getCommunityID();
			// 楼栋附件信息
			if (this.building.getAttachPath() != null && !"".equals(building.getAttachPath().trim())) {
				this.attachFileMap = this.iFileMappingService.queryEntityById(building.getAttachPath());
			}
		}
		// 所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if (this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		// 所属所属小区列表
		if (this.projectUID != null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		return "buildingManage";
	}

	/**
	 * 异步删除楼栋附件
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-11
	 */
	public String deleteBuildingAttach() throws Exception {
		this.rmsg = this.iBuildingService.deleteBuildingAttachFile(buildingUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}

	/**
	 * 新增或修改楼栋信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-9
	 */
	public String saveOrUpdateBuilding() throws Exception {
		this.rmsg = this.iBuildingService.saveOrUpdateBuilding(building, attachFile, attachFileFileName);
		this.buildingInfoSubmitResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}

	/**
	 * 楼栋信息提交响应信息
	 * @param rmsg
	 * @throws Exception
	 */
	private void buildingInfoSubmitResponse(String rmsg) throws Exception {
		StringBuffer resMsg = new StringBuffer("<script type='text/javascript'>");
		resMsg.append("parent.uploadCallBack('");
		resMsg.append(rmsg);
		resMsg.append("')");
		resMsg.append("</script>");
		this.writeJsonStrToResponse(resMsg.toString());
	}

	/**
	 * 根据楼栋唯一标识删除楼栋信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-9
	 */
	public String deleteBuildingByUID() throws Exception {
		this.rmsg = this.iBuildingService.deleteBuildingByUID(buildingUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}

	/**
	 * 通过楼栋所属关系(所属项目、所属小区)查询楼栋列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-20
	 */
	public String queryBuildingsByBelongs() throws Exception {
		this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectUID, communityUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(buildingList));
		return Action.SUCCESS;
	}
	
	/**
	 * 楼栋信息查看
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-8
	 */
	public String showBuildingDetail() throws Exception {
		// 楼栋信息
		if (this.buildingUID != null && !"".equals(buildingUID)) {
			this.building = this.iBuildingService.queryEntityById(buildingUID);
			// 楼栋附件信息
			if (this.building.getAttachPath() != null && !"".equals(building.getAttachPath().trim())) {
				this.attachFileMap = this.iFileMappingService.queryEntityById(building.getAttachPath());
			}
			//所属项目
			this.project = iProjectService.queryEntityById(building.getProjectID());
			//所属小区
			this.community = iCommunityService.queryEntityById(building.getCommunityID());
			//统计信息
			this.statisEntity = iBaseStatisService.queryBaseStatisEntity(BaseStatisConstants.statisType_buildingDetail, buildingUID);
		}
		return "buildingDetail";
	}
	
	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize = false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService
	 *            the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize = false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService
	 *            the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize = false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService
	 *            the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iFileMappingService
	 */
	@JSON(serialize = false)
	public IFileMappingService getiFileMappingService() {
		return iFileMappingService;
	}

	/**
	 * @param iFileMappingService
	 *            the iFileMappingService to set
	 */
	public void setiFileMappingService(IFileMappingService iFileMappingService) {
		this.iFileMappingService = iFileMappingService;
	}

	/**
	 * @return the iHeatRoomService
	 */
	@JSON(serialize = false)
	public IHeatRoomService getiHeatRoomService() {
		return iHeatRoomService;
	}

	/**
	 * @param iHeatRoomService the iHeatRoomService to set
	 */
	public void setiHeatRoomService(IHeatRoomService iHeatRoomService) {
		this.iHeatRoomService = iHeatRoomService;
	}

	/**
	 * @return the iBuildingUnitService
	 */
	@JSON(serialize = false)
	public IBuildingUnitService getiBuildingUnitService() {
		return iBuildingUnitService;
	}

	/**
	 * @param iBuildingUnitService the iBuildingUnitService to set
	 */
	public void setiBuildingUnitService(IBuildingUnitService iBuildingUnitService) {
		this.iBuildingUnitService = iBuildingUnitService;
	}

	/**
	 * @return the iBaseStatisService
	 */
	@JSON(serialize = false)
	public IBaseStatisService getiBaseStatisService() {
		return iBaseStatisService;
	}

	/**
	 * @param iBaseStatisService the iBaseStatisService to set
	 */
	public void setiBaseStatisService(IBaseStatisService iBaseStatisService) {
		this.iBaseStatisService = iBaseStatisService;
	}

	/**
	 * @return the buildingName
	 */
	public String getBuildingName() {
		return buildingName;
	}

	/**
	 * @param buildingName
	 *            the buildingName to set
	 */
	public void setBuildingName(String buildingName) {
		this.buildingName = buildingName;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList
	 *            the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex
	 *            the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond
	 *            the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize
	 *            the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the buildingPage
	 */
	public ResultWithPageCond<IBuilding> getBuildingPage() {
		return buildingPage;
	}

	/**
	 * @param buildingPage
	 *            the buildingPage to set
	 */
	public void setBuildingPage(ResultWithPageCond<IBuilding> buildingPage) {
		this.buildingPage = buildingPage;
	}

	/**
	 * @return the buildingUID
	 */
	public String getBuildingUID() {
		return buildingUID;
	}

	/**
	 * @param buildingUID
	 *            the buildingUID to set
	 */
	public void setBuildingUID(String buildingUID) {
		this.buildingUID = buildingUID;
	}

	/**
	 * @return the building
	 */
	public IBuilding getBuilding() {
		return building;
	}

	/**
	 * @param building
	 *            the building to set
	 */
	public void setBuilding(IBuilding building) {
		this.building = building;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList
	 *            the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	/**
	 * @return the pageProjectMap
	 */
	public Map<String, String> getPageProjectMap() {
		return pageProjectMap;
	}

	/**
	 * @param pageProjectMap
	 *            the pageProjectMap to set
	 */
	public void setPageProjectMap(Map<String, String> pageProjectMap) {
		this.pageProjectMap = pageProjectMap;
	}

	/**
	 * @return the pageCommunityMap
	 */
	public Map<String, String> getPageCommunityMap() {
		return pageCommunityMap;
	}

	/**
	 * @param pageCommunityMap
	 *            the pageCommunityMap to set
	 */
	public void setPageCommunityMap(Map<String, String> pageCommunityMap) {
		this.pageCommunityMap = pageCommunityMap;
	}

	/**
	 * @return the attachFileMap
	 */
	public IFileMapping getAttachFileMap() {
		return attachFileMap;
	}

	/**
	 * @param attachFileMap
	 *            the attachFileMap to set
	 */
	public void setAttachFileMap(IFileMapping attachFileMap) {
		this.attachFileMap = attachFileMap;
	}

	/**
	 * @return the attachFile
	 */
	public File getAttachFile() {
		return attachFile;
	}

	/**
	 * @param attachFile
	 *            the attachFile to set
	 */
	public void setAttachFile(File attachFile) {
		this.attachFile = attachFile;
	}

	/**
	 * @return the attachFileFileName
	 */
	public String getAttachFileFileName() {
		return attachFileFileName;
	}

	/**
	 * @param attachFileFileName
	 *            the attachFileFileName to set
	 */
	public void setAttachFileFileName(String attachFileFileName) {
		this.attachFileFileName = attachFileFileName;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID
	 *            the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the communityUID
	 */
	public String getCommunityUID() {
		return communityUID;
	}

	/**
	 * @param communityUID
	 *            the communityUID to set
	 */
	public void setCommunityUID(String communityUID) {
		this.communityUID = communityUID;
	}

	/**
	 * @return the buildingList
	 */
	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	/**
	 * @param buildingList
	 *            the buildingList to set
	 */
	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

	public List<IHeatRoom> getHeatRoomList() {
		return heatRoomList;
	}

	public void setHeatRoomList(List<IHeatRoom> heatRoomList) {
		this.heatRoomList = heatRoomList;
	}

	/**
	 * @return the statisEntity
	 */
	public IGenericStatisEntity getStatisEntity() {
		return statisEntity;
	}

	/**
	 * @param statisEntity the statisEntity to set
	 */
	public void setStatisEntity(IGenericStatisEntity statisEntity) {
		this.statisEntity = statisEntity;
	}

	/**
	 * @return the project
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * @param project the project to set
	 */
	public void setProject(IProject project) {
		this.project = project;
	}

	/**
	 * @return the community
	 */
	public ICommunity getCommunity() {
		return community;
	}

	/**
	 * @param community the community to set
	 */
	public void setCommunity(ICommunity community) {
		this.community = community;
	}
	
}
